//入口文件
import Vue from 'vue'
/* //按需导入
import { Header, Swipe, SwipeItem, Button, Lazyload} from 'mint-ui';
//实现图片软加载
Vue.use(Lazyload);
//Header
Vue.component(Header.name, Header);
//轮播图
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
//导入样式按钮
Vue.component(Button.name, Button);
//导入vue-resource */
//因软加载的bug，需全部导入
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)
//每次进入store之前 将本地存储中的数据赋值给car
var car = JSON.parse(localStorage.getItem('car') || "[]")
const store = new Vuex.Store({
    state:{
        car:car,
        /* 设计购物车中的商品对象{id:商品id，count：商品数量，price：商品单价，selected：是否被选中的状态} */
    },
    mutations:{
        addtocar(state,goodsinfo){
            var flag = false;
            state.car.some(item =>{
                if(item.id == goodsinfo.id){
                    item.count += parseInt(goodsinfo.count)
                    flag = true;
                    //终止some遍历
                    return true;
                }
            })
            if(!flag){
                state.car.push(goodsinfo)
            }
            //将数据放到本地存储中形成数据持久化
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item =>{
                if(item.id == goodsinfo.id){
                    item.count = parseInt(goodsinfo.count)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFromCar(state,id){
            state.car.forEach((item,i) =>{
                if(item.id ==id){
                    state.car.splice(i,1)
                    return
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsSelected(state,info){
            state.car.some(item =>{
                if(item.id == info.id){
                    item.selected =info.selected
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{
        getAllCount(state){
            var c = 0;
            state.car.forEach(item =>{
                c +=item.count
            })
            return c;
        },
        getGoodsCount(state){
            var o = [];
            state.car.forEach(item =>{
                o[item.id] = item.count
            })
            return o;
        },
        getGoodsSelected(state){
            var o = [];
            state.car.forEach(item =>{
                o[item.id] = item.selected;
            })
            return o;
        },
        getGoodsCountAndAmount(state){
            var o ={
                count:0,
                amount:0
            }
            state.car.forEach(item =>{
              if(item.selected){
                o.count +=item.count
                o.amount += item.price * item.count
                }  
            })
            return o;
        }
    }
})

import VueResource from 'vue-resource'
Vue.use(VueResource)
//设置默认请求根路径
Vue.http.options.root = 'http://www.liulongbin.top:3005/'
//全局设置post时候的表单数据格式
Vue.http.options.emulateJSON = true;

//导入格式化时间的插件
import moment from 'moment'
//定义全局的时间过滤器
Vue.filter('dataFormat',function(dataStr,pattern='YYYY-MM-DD HH:mm:ss'){
    return moment(dataStr).format(pattern)
})
//导入图片预览插件
import VuePreview from 'vue2-preview'
Vue.use(VuePreview)

import app from './App.vue'
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import './lib/mui/fonts/mui-icons-extra.ttf'

//导入路由模块
import Vuerouter from "vue-router"
Vue.use(Vuerouter)

import router from './router.js'

var vm = new Vue({
    el:"#app",
    render: c => c(app),
    router,
    store
})
